import 'package:loxen/api/http_server/user_comment.dart';
import 'package:loxen/mode/mine/mine_comment_list.dart';
import 'package:loxen/viewmode/base_view_model.dart';
import 'package:rxdart/rxdart.dart';

class MineCommentViewModel extends BaseViewModel with UserCommentMixin {
  Map<String, List<MineComment>> _map = {};

  //加载数据
  void loadData() {
    userComment().doOnListen(() {
      mState = BaseState.LOADING;
      notifyListeners();
    }).listen((event) {
      if (event.results.length > 0) {
        _map.putIfAbsent('0', () => event.results);
        mState = BaseState.CONTENT;
      } else {
        mState = BaseState.EMPTY;
      }
    }, onError: (error, stackTrace) {
      mState = BaseState.FAIL;
      print(error);
      print(stackTrace);
    }, onDone: () => notifyListeners());
  }

  // 加载下一页数据
  Future<List<MineComment>> nextData(int page) async {
    if (_map.containsKey(page.toString())) {
      return _map[page.toString()];
    } else {
      var data = await userComment(page: (page += 1).toString()).first;
      _map.putIfAbsent(page.toString(), () => data.results);
      return data.results;
    }
  }

  //刷新列表内容
  void refresh() {
    _map.clear();
  }
}
